;
(function($,win)
{
	/* @------------------------------------------------------------------------------------@ */
	$(document).ready(function()
	{
		var url = location.search;
		if (url.indexOf("?userId=") != -1) {
			var reqPath = basePath+'userManager/queryUserById.action'+url;
			var sendBody = '{}';
			send(reqPath, "POST", "application/json", "json", sendBody, function(res)
			{
				if (res.result == 'success')
				{
					if (res.data != null)
					{
						debugger;
						var user = res.data;
						$("#uu_id").val(user.uuid);
						$("#user_name").val(user.userName);
						$("#pass_word").val(user.password);
						$("#qq_name").val(user.qqName);
						$("#ph_one").val(user.phone);
						$("#tele_phone").val(user.telephone);
						$("#user_type").val(user.userType);
						$("#user_score").val(user.score);
						if(user.userPower=='1')
						{
							$("#userpower_1").checked=true;
						}else if (user.userPower=='0')
						{
							$("#userpower_0").checked=true;
						}
					} else
					{
						alert("没有查找到相关数据");
					}
				}
			});
		}else
		{
			//初始化列表
			iniUserData(0,15);
		}
		$('#addnew').click(function()
		{

			window.location.href = jumpPath + 'view/system/user/adduser.jsp';
		});

		// 查询
		$('#btn_search').bind('click', function()
		{
			iniUserData(0,15);
		});

		$('#tbn_save').bind('click', function()
		{
			saveLoginUser();
		});
		
		$('#tbn_update').bind('click', function()
		{
			updateLoginUser();
		});

	});// the end

	/* @-----------------------------------------------------------------------------------------@ */
})(jQuery, window);

function editUser(userId)
{
	window.location.href = jumpPath + 'view/system/user/edituser.jsp?userId='+userId;
}

function del(id)
{
	if (confirm("确定要删除吗？"))
	{
		var reqPath = basePath+'userManager/deleteUser.action';
		var sendBody = '{"uuid": "'+id+'"}';
		send(reqPath, "POST", "application/json", "json", sendBody, function(res)
		{
			if (res.result == 'success')
			{
				iniUserData(0,15);
			}
		});
	}
}

function iniUserData(pageNumber,pagesize)
{
	debugger;
	var reqPath = basePath+'userManager/queryUserByPage.action';
	var sendBody = '{"currentPage": 1,"pageSize": 15}';
	var $tab = $('#userDataTables tbody').html("");

	
	var reqPath = basePath + 'userManager/queryUserByPage.action';
	var sendBody = '{"currentPage": '+pageNumber+',"pageSize": '+pagesize+'}';
	var $tab = $('#userDataTables tbody').html("");

	send(reqPath, "POST", "application/json", "json", sendBody, function(res)
	{

		if (res.result == 'success')
		{

			if (res.data.data.length > 0)
			{
				var tpl_tr = $('#tpl_list').html(), rows = '';

				$.each(res.data.data, function(i,n)
				{
					var userPower = '无';
					var userType = '普通用户';
					if(null!=n.userPower&&n.userPower==1)
					{
						userPower = '有';
					}
					if(null!=n.userType&&n.userType==1)
					{
						userType = '管理员';
					}
					var operation="<button onclick='editUser(\""+n.uuid+"\")' type='button' class='btn btn-primary'>编辑</button>"+
								  "<button onclick='del(\""+n.uuid+"\")' type='button' class='btn btn-primary'>删除</button>";
					rows += tpl_tr.replace(/\$uuid/g, n.uuid)
							      .replace(/\$userName/,n.userName)
							      .replace(/\$qqName/, n.qqName)
							      .replace(/\$phone/, n.phone)
							      .replace(/\$userPower/, userPower)
							      .replace(/\$userType/, userType)
							      .replace(/\$userScore/, n.score)
							      .replace(/\$operation/, operation);
				});
				$tab.append(rows);

				// page
				pagefunction(iniUserData,res.data.totalPages,pagesize);

			} else
			{
				$tab.append('<tr><td colspan="7">没有查找到相关数据。</td></tr>');
			}
		}
	});

}

/**
 * 
 * @METHODNAME: saveLoginUser
 * @METDHOD:
 * @Description: 添加登陆用户
 * @author Administrator
 * @date 2015年1月10日 下午5:37:52
 */
function saveLoginUser()
{
	
	var requsetPath = basePath + "userManager/insertUser.action";
	var requestData = $('#frm_login_user').serializeObject();
	requestData = JSON.stringify(requestData);
	// alert(requestData);
	send(requsetPath, "POST", "application/json", "json", requestData,
			function(resultData)
			{

				if (resultData.result == 'success')
				{
					// 跳转到列表
					window.location = jumpPath
							+ 'view/system/user/userlist.jsp';
				} else
				{
					alert(resultData.errorMsg);
				}
			});

}


function updateLoginUser()
{
	
	var requsetPath = basePath + "userManager/updateUser.action";
	var requestData = $('#frm_login_user').serializeObject();
	requestData = JSON.stringify(requestData);
	// alert(requestData);
	send(requsetPath, "POST", "application/json", "json", requestData,
			function(resultData)
			{

				if (resultData.result == 'success')
				{
					// 跳转到列表
					window.location = jumpPath
							+ 'view/system/user/userlist.jsp';
				} else
				{
					alert(resultData.errorMsg);
				}
			});

}
